package com.hjxr.xmgl.web.controller;

import java.util.ArrayList;
import java.util.List;

import com.hjxr.framework.web.domain.AjaxResult;
import com.hjxr.sysadmin.uitl.poi.ExcelUtil;
import com.hjxr.sysadmin.web.page.TableDataInfo;
import com.hjxr.xmgl.web.domain.EventQuotaFunctionQueue;
import com.hjxr.xmgl.web.service.IEventQuotaFunctionQueueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hjxr.framework.aspectj.lang.annotation.Log;
import com.hjxr.framework.aspectj.lang.enums.BusinessType;


/**
 * 数据函数运算队列Controller
 *
 * @author hjxr
 * @date 2021-12-02
 */
@Api("事件管理")
@RestController
@RequestMapping("/event/queue")
public class EventQuotaFunctionQueueController extends BaseController
{
    @Autowired
    private IEventQuotaFunctionQueueService eventQuotaFunctionQueueService;

    /**
     * 查询数据函数运算队列列表
     */
    @PreAuthorize("@ss.hasPermi('system:queue:list')")
    @GetMapping("/list")
    public TableDataInfo list(EventQuotaFunctionQueue eventQuotaFunctionQueue)
    {
        startPage();
        List<EventQuotaFunctionQueue> list = eventQuotaFunctionQueueService.selectEventQuotaFunctionQueueList(eventQuotaFunctionQueue);
        return getDataTable(list);
    }

    /**
     * 导出数据函数运算队列列表
     */
    @PreAuthorize("@ss.hasPermi('system:queue:export')")
    @Log(title = "数据函数运算队列", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EventQuotaFunctionQueue eventQuotaFunctionQueue)
    {
        List<EventQuotaFunctionQueue> list = eventQuotaFunctionQueueService.selectEventQuotaFunctionQueueList(eventQuotaFunctionQueue);
        ExcelUtil<EventQuotaFunctionQueue> util = new ExcelUtil<EventQuotaFunctionQueue>(EventQuotaFunctionQueue.class);
        return util.exportExcel(list, "queue");
    }

    /**
     * 获取数据函数运算队列详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:queue:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(eventQuotaFunctionQueueService.selectEventQuotaFunctionQueueById(id));
    }

    /**
     * 新增数据函数运算队列
     */
    @PreAuthorize("@ss.hasPermi('system:queue:add')")
    @Log(title = "数据函数运算队列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EventQuotaFunctionQueue eventQuotaFunctionQueue)
    {
        return toAjax(eventQuotaFunctionQueueService.insertEventQuotaFunctionQueue(eventQuotaFunctionQueue));
    }

    /**
     * 修改数据函数运算队列
     */
    @PreAuthorize("@ss.hasPermi('system:queue:edit')")
    @Log(title = "数据函数运算队列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EventQuotaFunctionQueue eventQuotaFunctionQueue)
    {
        return toAjax(eventQuotaFunctionQueueService.updateEventQuotaFunctionQueue(eventQuotaFunctionQueue));
    }

    /**
     * 删除数据函数运算队列
     */
    @PreAuthorize("@ss.hasPermi('system:queue:remove')")
    @Log(title = "数据函数运算队列", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(eventQuotaFunctionQueueService.deleteEventQuotaFunctionQueueByIds(ids));
    }

    /**
     * 定时执行预警函数
     */

    @ApiOperation("执行事件")
    @GetMapping(value = "/execute")
    @ResponseBody
    public void execute(EventQuotaFunctionQueue eventQuotaFunctionQueue){
        List<EventQuotaFunctionQueue> list = eventQuotaFunctionQueueService.selectEventQuotaFunctionQueueList(eventQuotaFunctionQueue);
        for (EventQuotaFunctionQueue EventQuotaFunctionQueue: list ) {
            eventQuotaFunctionQueueService.execute(EventQuotaFunctionQueue);
        }

    }

    /**
     * 定时执行预警函数
     */

    @ApiOperation("已经注册函数")
    @GetMapping(value = "/functionList")
    @ResponseBody
    public AjaxResult  functionList(){
        List list =new ArrayList();
        list.add("add");
        list.add("reduce");
        list.add("multiply");
        return AjaxResult.success(list);
    }

}
